package com.ctg.itrdc.sysmgr.portal.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ctg.itrdc.pasp.context.service.BaseService;
import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemPostService;
import com.ctg.itrdc.sysmgr.portal.data.ISystemPostDao;
import com.ctg.itrdc.sysmgr.portal.data.entity.SystemPost;
import com.ctg.itrdc.sysmgr.portal.data.vo.SystemPostVO;
import com.ctg.itrdc.sysmgr.portal.service.exception.SysmgrServiceException;
import com.ctg.itrdc.sysmgr.portal.service.utils.BeanUtils;

@Component("systemPostService")
public class SystemPostServiceImpl extends BaseService<SystemPost, Long, SystemPostDTO>
		implements ISystemPostService {
	@Resource
	private ISystemPostDao systemPostDao;
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Long insert(SystemPostDTO dto) {
		try {
			long id = super.insert(dto);
			//去掉 岗位编号自动生成的逻辑 xkw 20160808
//		SystemPostDTO tmp = new SystemPostDTO();
//		tmp.setId(id);
//		tmp.setSysPostCode(RandomCodeUtil.gen(RandomCodeUtil.SYSTEM_POST, id));
//		this.updateNotNullFieldsByPrimaryKey(tmp);
			return id;
		} catch (DuplicateKeyException e) {
			throw new SysmgrServiceException("岗位编码重复");
		}
	}

	@Override
	public List<SystemPostDTO> selectByUId(Long sysUserId) {
		List<SystemPostVO> entityList = systemPostDao.selectByUId(sysUserId);
		List<SystemPostDTO> dtoList = BeanUtils.copyListNotException(
				entityList, SystemPostDTO.class);
		logger.debug(
				"find systemPostDTOList from DB,param:sysUserId:{},result:{}",
				sysUserId, JSON.toJSONString(dtoList, true));
		return dtoList;
	}


	@SuppressWarnings("all")
	@Override
	public PageInfo queryPageInfo(SystemPostDTO dto, Long sysUserId,Integer currentPage,
			Integer perPageNum) {
		SystemPost entity = BeanUtils.applyIfNotException(dto, SystemPost.class);
		PageInfo pageInfo = null;
		if(dto.getRzOrgId()!=null){
			pageInfo = systemPostDao.jdbcFindPageInfoByRzOrgId(entity,dto.getRzOrgId(),sysUserId,currentPage, perPageNum);
		}else{
			// 默认根据管理orgId查询
			pageInfo = systemPostDao.jdbcFindPageInfoByOrgId(entity,sysUserId, currentPage, perPageNum);
		}
		if(pageInfo!=null){
			List<SystemPost> list = pageInfo.getList();
			List<SystemPostDTO> dtoList = BeanUtils.copyListNotException(list, SystemPostDTO.class);
			pageInfo.setList(dtoList);
		}
	
		return pageInfo;
	}

	@Override
	public int updateByPrimaryKey(SystemPostDTO dto) {
		try {
			return super.updateByPrimaryKey(dto);
		} catch (DuplicateKeyException e) {
			throw new SysmgrServiceException("岗位编码重复");
		}
	}
	
	@Transactional
	public int deleteByPrimaryKey(SystemPostDTO dto) {
		return super.deleteByPrimaryKey(dto);
	}
	
	@Transactional
	public int deleteByPrimaryKey(Long id) {
		return super.deleteByPrimaryKey(id);
	}
	
	@Transactional
	public int[] batchDeleteByPrimaryKey(List<SystemPostDTO> list) {
		return batchDeleteByPrimaryKey(list);
	}
	
}
